package com.huat.controller;



import com.huat.mapper.UserMsgMapper;
import com.huat.pojo.UserMsg;
import com.huat.service.Impl.UserMsgServiceImpl;
import com.huat.service.UserMsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.HttpRequestHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;

@Controller
public class UserMsgController {

    @Autowired
    UserMsgServiceImpl userMsgServiceimpl;
    @Autowired
    UserMsgMapper userMsgMapper;
    @Autowired
    UserMsgService userMsgService;
    @Autowired
    UserMsg userMsg;

    @RequestMapping("/usermsg/getall")
    public String getAllUserMsg(Model model) {
        ArrayList<UserMsg> userMsgs = userMsgServiceimpl.getAllUserMsg();
        for (UserMsg serMsgs : userMsgs
        ) {
            System.out.println(serMsgs.getSclass());
        }

        model.addAttribute("usermsgs", userMsgs);
        return "studentInfo";
    }

    @RequestMapping("/usermsg/delete")
    public String deleteUserMsg(@RequestParam("userid") String userid, HttpSession session) {
        boolean flog;
        if (userMsgServiceimpl.deleteUserMsg(userid)) {
            flog = true;
        } else {
            flog = false;
        }
        session.setAttribute("flog", flog);
        return "redirect:/usermsg/getall";
    }

    @RequestMapping("/usermsg/revise")
    public String findUserMsg(@RequestParam("email") String email, Model model) {
        UserMsg userMsg = userMsgServiceimpl.findUserMsg(email);
        model.addAttribute("usermsg", userMsg);
        return "reviseStudentInfo";
    }

    @RequestMapping("/usermsg/submit")
    public String submitUserMsg(@RequestParam("userid")String userid, @RequestParam("role")String role, @RequestParam("grend")String grend, @RequestParam("age")String age, @RequestParam("clano")String clano, @RequestParam("dormitory")String dormitory, @RequestParam("email")String email, HttpSession session){
        userMsg.setUserId(userid);
        userMsg.setRole(role);
        userMsg.setGrend(grend);
        userMsg.setAge(Integer.parseInt(age));
        userMsg.setClano(clano);
        userMsg.setDormitory(dormitory);
        userMsg.setEmail(email);
        userMsgService.submitUserMsg(userMsg);
        session.setAttribute("userid",userid);
        return "redirect:/index.html";
    }
}
